package team.se.tms.dao.impl;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import team.se.tms.dao.Dao;
import team.se.tms.exception.DeleteException;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.SelectException;
import team.se.tms.exception.UpdateException;

import javax.annotation.Resource;
import java.util.List;

/**
 * Dao实现层，具体实现dao接口层定义的方法
 *
 * @author winkyle
 * @date 2018/11/1.
 */
@Slf4j
@Repository("DaoImpl")
public class DaoImpl implements Dao {

    @Resource(name = "sqlSessionTemplate")
    private SqlSessionTemplate sqlSessionTemplate;

    /**
     * 插入记录
     *
     * @param statement 要使用的mapper
     * @param parameter 参数
     * @return 插入结果
     * @throws Exception 异常
     * @create on 2018/11/1 by winkyle
     */
    @Override
    public int insert(String statement, Object parameter) throws Exception {
        int insertResult = 0;
        try {
            insertResult = sqlSessionTemplate.insert(statement, parameter);
        }catch (Exception e){
            e.printStackTrace();
            throw new InsertException();
        }
        return insertResult;
    }

    /**
     * 删除记录
     *
     * @param statement 要使用的mapper
     * @param parameter 参数
     * @return 删除结果
     * @throws Exception 异常
     * @create on 2018/11/1 by winkyle
     */
    @Override
    public int delete(String statement, Object parameter) throws Exception {
        int deleteResult = 0;
        try {
            deleteResult = sqlSessionTemplate.delete(statement, parameter);
        }catch (Exception e){
            e.printStackTrace();
            throw new DeleteException();
        }
        return deleteResult;
    }

    /**
     * 更新记录
     *
     * @param statement 要使用的mapper
     * @param parameter 参数
     * @return 更新结果
     * @throws Exception 异常
     * @create on 2018/11/1 by winkyle
     */
    @Override
    public int update(String statement, Object parameter) throws Exception {
        int updateResult = 0;
        try {
            updateResult = sqlSessionTemplate.update(statement, parameter);
        }catch (Exception e){
            e.printStackTrace();
            throw new UpdateException();
        }
        return updateResult;
    }

    /**
     * 查找一条记录
     *
     * @param statement 要使用的mapper
     * @param parameter 参数
     * @return 查到的一条记录
     * @throws Exception 异常
     * @create on 2018/11/1 by winkyle
     */
    @Override
    public <T> T selectOne(String statement, Object parameter) throws Exception {
        T selectResult = null;
        try {
            selectResult = sqlSessionTemplate.selectOne(statement, parameter);
        }catch (Exception e){
            e.printStackTrace();
            throw new SelectException();
        }
        return selectResult;
    }

    /**
     * 查找多条记录
     *
     * @param statement 要使用的mapper
     * @param parameter 参数
     * @return 查到的多条记录
     * @throws Exception 异常
     * @create on 2018/11/1 by winkyle
     */
    @Override
    public <T> List<T> selectList(String statement, Object parameter) throws Exception {
        List<T> selectResult = null;
        try {
            selectResult = sqlSessionTemplate.selectList(statement, parameter);
        }catch (Exception e){
            e.printStackTrace();
            throw new SelectException();
        }
        return selectResult;
    }
}
